package com.tos.controller;



import com.tos.pojo.LoginForm;
import com.tos.pojo.Passenger;

import com.tos.service.PassengerService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/passenger")
public class PassengerController {
    @Autowired
    private PassengerService passengerService;


    @RequestMapping("/login")
    public Passenger login(@RequestBody LoginForm loginForm) {

        Passenger passenger = passengerService.login(loginForm.getPhone(),loginForm.getPassword());
        if (passenger == null) {
            return null;

        }
        System.out.println(passenger);
        return passenger;


    }


    @RequestMapping("/register")
    public String register(@RequestBody Passenger passenger) {
        HashMap<String,Object> map = new HashMap<>();


        map.put("cardId",passenger.getCardId());
        map.put("passengerName",passenger.getPassengerName());
        map.put("phone",passenger.getPhone());
        map.put("password",passenger.getPassword());

        int result = passengerService.register(map);
        System.out.println("result = " + result);
        if (result == 0) {
            return "success";
        }else if(result == 1){
           return "注册失败!号码已被注册!";
        }else if(result == 2) {
            return "注册失败!身份证已存在!";
        }else {
            return "注册失败!数据库错误!";
        }

    }

    @RequestMapping("/query")
    public Passenger query(@RequestBody Passenger passenger){
        return passengerService.getPassengerByPhone(passenger.getPhone());
    }

    @RequestMapping("/getPassengers")
    public List<Passenger> getPassengers(){

        return passengerService.getPassengers();
    }

    @RequestMapping("/editInfo")
    public String updateInfo(@RequestBody Passenger passenger){

        System.out.println(passenger);
        HashMap<String,Object> map = new HashMap<>();

        map.put("passengerId",passenger.getPassengerId());
        map.put("cardId",passenger.getCardId());
        map.put("passengerName",passenger.getPassengerName());
        map.put("phone",passenger.getPhone());
        map.put("password",passenger.getPassword());
        map.put("nickName",passenger.getNickName());
        map.put("city",passenger.getCity());
        map.put("email",passenger.getEmail());
        map.put("wechat",passenger.getWechat());
        map.put("password",passenger.getPassword());
        //更新数据库用户信息
        boolean success = passengerService.updateInfo(map);
        if(success){
            return "success";
        }
        return "fail";
    }







}
